const path = require('path');
const fs = require('fs');

/**
 * 修改 webpack/lib/ExternalModule.js -> getSourceForScriptExternal
 * ${JSON.stringify(url)} 替换为 ${url}
 */
(function () {
  const ExternalModulePath = path.resolve(process.cwd(), 'node_modules/webpack/lib/ExternalModule.js');
  const stat = fs.statSync(ExternalModulePath);
  if (stat.isFile()) {
    let content = fs.readFileSync(ExternalModulePath, { encoding: 'utf-8' });
    const regex = /\$\{JSON\.stringify\(\s*url\s*\)\}/s;
    if (regex.test(content)) {
      fs.writeFileSync(ExternalModulePath, content.replace(regex, `\${url}`));
    }
  }
})();
